Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to SocketPool #198

Merged
merged 2 commits into from
Apr 30, 2024
Merged

Conversation

justmobilize
Copy link
Collaborator

@justmobilize justmobilize commented Apr 25, 2024

For use with: adafruit/Adafruit_CircuitPython_ConnectionManager#11

Converting from the old adafruit_esp32spi/adafruit_esp32spi_socket.py to adafruit_esp32spi/adafruit_esp32spi_socketpool.py to remove set_interface and work more like a built-in SocketPool.

This also allows one to use multiple ESP32SPI boards at the same time.

General example:

import board
import busio
import digitalio
import os
import adafruit_requests
from adafruit_esp32spi.adafruit_esp32spi import ESP_SPIcontrol
import adafruit_esp32spi.adafruit_esp32spi_socketpool as socketpool

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")

esp32_cs = digitalio.DigitalInOut(board.D13)
esp32_ready = digitalio.DigitalInOut(board.D11)
esp32_reset = digitalio.DigitalInOut(board.D12)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
radio = ESP_SPIcontrol(spi_bus, esp32_cs, esp32_ready, esp32_reset)
radio.connect_AP(wifi_ssid, wifi_password)
pool = socketpool.SocketPool(radio)
requests = adafruit_requests.Session(pool)
print(requests.get(TEXT_URL).text)

or with the new ConnectionManager

import board
import busio
import digitalio
import os
import adafruit_connection_manager
import adafruit_requests
from adafruit_esp32spi.adafruit_esp32spi import ESP_SPIcontrol
from adafruit_esp32spi import adafruit_esp32spi_socketpool

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")

esp32_cs = digitalio.DigitalInOut(board.D13)
esp32_ready = digitalio.DigitalInOut(board.D11)
esp32_reset = digitalio.DigitalInOut(board.D12)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
radio = ESP_SPIcontrol(spi_bus, esp32_cs, esp32_ready, esp32_reset)
radio.connect_AP(wifi_ssid, wifi_password)
pool = adafruit_connection_manager.get_radio_socketpool(radio)
assert isinstance(pool, adafruit_esp32spi_socketpool.SocketPool)
requests = adafruit_requests.Session(pool)
print(requests.get(TEXT_URL).text)

@justmobilize justmobilize force-pushed the convert-to-socketpool branch from 646d60c to 35fd193 Compare April 25, 2024 02:50
@anecdata
Copy link
Member

Looks good!

Config:
Adafruit CircuitPython 9.0.0-4-g5dadc13cac on 2024-03-21; FeatherS3 with ESP32S3
Airlift FeatherWing
adafruit_esp32spi PR 198 (this PR)
adafruit_connection_manager PR 11 (Use new SocketPool for ESP32SPI and WIZNET5K)
(all other libraries: latest release)

code...
import time
import os
import traceback
import board
import busio
import digitalio
import os
import adafruit_connection_manager
import adafruit_requests
from adafruit_esp32spi.adafruit_esp32spi import ESP_SPIcontrol

URLS = ("http://wifitest.adafruit.com/testwifi/index.html", "https://httpbin.org/get")

def get_requests(radio):
    # this allows creating multiple requests sessions for multiple devices
    pool = adafruit_connection_manager.get_radio_socketpool(radio)
    ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
    requests = adafruit_requests.Session(pool, ssl_context)
    return requests

def connect_esp(radio):
    while not radio.is_connected:
        try:
            radio.connect_AP(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
        except Exception as ex:
            traceback.print_exception(ex, ex, ex.__traceback__)
    ipv4_str = radio.pretty_ip(radio.ip_address)
    return ipv4_str

spi = board.SPI()
esp32_cs = digitalio.DigitalInOut(board.A0)
esp32_reset = digitalio.DigitalInOut(board.A1)
esp32_ready = digitalio.DigitalInOut(board.A2)
radio = ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False)
requests_esp = get_requests(radio)
while True:
    print(f'{"="*25}')
    for url in URLS:
        print(f'{"-"*25}')
        print(f"Fetching from {url} via ESP32SPI {connect_esp(radio)} ", end="")
        with requests_esp.get(url) as resp:
            print(f'{resp.status_code} {resp.reason.decode()}')
        time.sleep(5)

@dhalbert
Copy link
Contributor

This kind of bypasses https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI/blob/main/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py. Do you see that as being supplanted by ConnectionManager eventually? It does have functionality CM does not.

I was interested in replacing the secrets arg in wifimanager with something that looked up settings in settings.toml, if secrets, and/or replacing secrets with separate ssid and password args. But I haven't looked in great detail at this: it was something I noticed while trying to get rid of uses of secrets.py in Learn Guides.

@anecdata
Copy link
Member

anecdata commented Apr 25, 2024

To me so far, Connection Manager is all about sockets, and it's shaping up to be a very simple API, versatile for single- and multiple-network configurations. I seem to recall a discussion about whether it should take on additional features like wifi connection, but don't remember the conclusion. I couldn't find anything in In The Weeds from this year. I have some concern about CM getting too big with disparate features.

@justmobilize
Copy link
Collaborator Author

@dhalbert I would love to take out the rest of secrets. If you help me come up with the right path, I take it and run

@dhalbert
Copy link
Contributor

@anecdata Thanks for that perspective.
@justmobilize Then I would just do what I said above: modernize wifi manager, and finish cleaning up the remaining guides. Not urgent, but I will look at this relatively soon. I agree about keeping CM small for now.

@justmobilize
Copy link
Collaborator Author

@dhalbert wifi manager is already using the ConnectionManager (here), so would these changes should all automatically apply

@justmobilize justmobilize marked this pull request as ready for review April 28, 2024 04:38
Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-tested with adafruit/Adafruit_CircuitPython_ConnectionManager#11 (review). Good to go pending any others with code review

@dhalbert
Copy link
Contributor

Looks good, but is this an incompatible change? If so, then we need to update Guide code right away after merging this.

@justmobilize
Copy link
Collaborator Author

Only if they aren't using ConnectionManager. Here's the only guide we need to update: https://github.com/search?q=repo%3Aadafruit%2FAdafruit_Learning_System_Guides+adafruit_esp32spi_socket&type=code

@dhalbert
Copy link
Contributor

OK, let's merge this, and then if you can make a PR to the Learn Guide repo, that would be great.

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Updating ConnectionManager at same time.

@dhalbert dhalbert merged commit a48d516 into adafruit:main Apr 30, 2024
1 check passed
@justmobilize justmobilize deleted the convert-to-socketpool branch April 30, 2024 16:09
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request May 1, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI to 8.0.0 from 7.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#198 from justmobilize/convert-to-socketpool

Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 7.0.0 from 6.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#159 from justmobilize/convert-to-socketpool

Updating https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager to 2.0.0 from 1.2.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_ConnectionManager#11 from justmobilize/esp32spi-and-wiznet5k-socketpool

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants